﻿using AutoStar.Common;
using AutoStar.ViewModel;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using static AutoStar.Models.PrintSliceModel;

namespace AutoStar.Views.Pages
{
    /// <summary>
    /// PrintSliceView.xaml 的交互逻辑
    /// </summary>
    public partial class PrintSliceView : Page
    {
        //private PrintSliceVM VM;
        public PrintSliceView()
        {
            InitializeComponent();
            //VM = new PrintSliceVM();
            DataContext = Global.SliceVM;
        }
        private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataGrid dataGrid = sender as DataGrid;
            //if (dataGrid != null && dataGrid.SelectedItems.Count > 0)
            //{
            //    PrintSliceItem selectedItem = dataGrid.SelectedItem as PrintSliceItem;
            //    if (selectedItem != null)
            //    {
            //        try
            //        {
            //            Global.SliceVM.DataGrid_SelectionChanged(selectedItem.层序号);
            //        }
            //        catch
            //        {
            //        }
            //    }
            //}
            if (dataGrid != null && dataGrid.SelectedItems.Count > 0)
            {
                // 获取所有选中的项
                foreach (var selectedItem in dataGrid.SelectedItems)
                {
                    PrintSliceItem printSliceItem = selectedItem as PrintSliceItem;
                    if (printSliceItem != null)
                    {
                        try
                        {
                            // 处理每个选中的项
                            Global.SliceVM.DataGrid_SelectionChanged(printSliceItem.层序号);
                        }
                        catch
                        {
                            // 异常处理
                        }
                    }
                }

                // 如果需要获取所有选中的行的索引
                List<int> selectedIndices = new List<int>();
                foreach (var selectedItem in dataGrid.SelectedItems)
                {
                    int index = dataGrid.Items.IndexOf(selectedItem);
                    if (index != -1)
                    {
                        selectedIndices.Add(index);
                    }
                }
                Global.SliceVM.SelectedIndices = selectedIndices;
            }
        }
        private float baseWidth = 0;
        private float baseHeight = 0;
        private void Page_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (baseWidth == 0)
            {
                baseWidth = (float)e.NewSize.Width; // 设置基础宽度
                baseHeight = (float)e.NewSize.Height; // 设置基础高度
            }
            this.FontSize = e.NewSize.Width / 70;
            Global.SliceVM.ViewWidth = (float)(e.NewSize.Width / baseWidth);
            Global.SliceVM.ViewHeight = (float)(e.NewSize.Height / baseHeight);
            //Global.SliceVM.ViewWidth = (float)(e.NewSize.Width / (e.PreviousSize.Width == 0 ? e.NewSize.Width : e.PreviousSize.Width));
            //Global.SliceVM.ViewHeight = (float)(e.NewSize.Height / (e.PreviousSize.Height == 0 ? e.NewSize.Height : e.PreviousSize.Height));
            foreach (var child in ButtonsGrid.Children)
            {
                if (child is Button button)
                {
                    button.FontSize = e.NewSize.Width / 50;
                    if (e.PreviousSize.Width > 0)
                        button.Width = e.NewSize.Width / e.PreviousSize.Width * button.Width;
                }
            }
        }

    }
}
